package control.manager
{
	import control.util.VOUtil;
	
	import data.GameConst;
	import data.PlayerData;
	import data.TaskVO;
	import data.UserTaskVO;
	
	import flash.utils.Dictionary;
	
	import view.layer.GameLayer;
	import view.role.MapMonster;
	import view.role.MapNPC;
	import view.role.MapPlayer;
	import view.ui.UITaskTrack;

	/**
	 * 任务管理器 
	 * @author leo
	 * 
	 */	
	public class TaskManager
	{
		private static var _instance :TaskManager = null;
		/**
		 * 总表任务信息 
		 */		
		public var taskList :Array;
		public function TaskManager(prv :PrivateClass)
		{
		}

		public static function get instance():TaskManager
		{
			if(_instance == null)
			{
				_instance = new TaskManager(new PrivateClass());
			}
			return _instance;
		}
		/**
		 * 检查战斗任务是否够条件完成
		 */		
		public function checkTaskIsCompelte(monster :MapMonster):void
		{
			var playerData :PlayerData = RoleManager.instance.playerData;
			var monsterID :String = monster.monsterData.monsterID;
			for each(var userTaskVO :UserTaskVO in playerData.taskingList)
			{
				if(userTaskVO.taskSort != TaskVO.BATTLE_SORT) continue;
				if(userTaskVO.taskVO.battleTaskData.length == 0) continue;
				if(userTaskVO.battleType != TaskVO.KILL_MONSTER) continue;
				if(userTaskVO.canComplete) continue;
				
				if(userTaskVO.battleObjID == monsterID)
				{
					userTaskVO.battleNum ++;
				}
				
				if(userTaskVO.battleNum >= userTaskVO.taskVO.battleTaskData[2])
				{
					//可以完成
					var npc :MapNPC = RoleManager.instance.findNPCByID(userTaskVO.completeID);
					if(npc)
					{
						npc.setTaskStatus(GameConst.CAN_COMPLETE);
					}else
					{
						//如果没有找到NPC，检查下是否需要是其他地图的NPC
						RoleManager.instance.isNeedChangeMapByNPC(userTaskVO.completeID);
					}
					userTaskVO.canComplete = true;
					UITaskTrack.I.acceptTask();
					
				}
				
			}
			
		}
		
		/**
		 * 初始化总表任务数据 
		 * 
		 */		
		public function initTaskList():void
		{
			var dict :Dictionary = LoaderManager.instance.txtDict;
			var str :String = dict["task"];
			
			this.taskList = VOUtil.I.createList(str,TaskVO);
			
			
			findCanAcceptTaskList();
		}
		/**
		 * 查找当前NPC身上的所有任务 
		 * @param npcID
		 * @return 
		 * 
		 */		
		public function findCurNPCTask(npcID :String):Array
		{
			var result :Array;
			if(this.taskList && this.taskList.length > 0)
			{
				result = [];
				for each(var taskVO :TaskVO in this.taskList)
				{
					if(taskVO.sendNPCID == npcID)
					{
						result.push(taskVO);
					}
				}
			}
			
			return result;
		}
		/**
		 * 所有可以接的任务 
		 * @return 
		 * 
		 */		
		public function findCanAcceptTaskList():Array
		{
			var result :Array = [];
			//已经完成和进行中的任务id列表
			var userTaskList :Array = [];
			var userTaskVO :UserTaskVO;
			var playerData :PlayerData = RoleManager.instance.playerData;
			if(!playerData)
			{
				return result;
			}
			//完成的任务
			for each(userTaskVO in playerData.completeList)
			{
				userTaskList.push(userTaskVO.taskID);
			}
			//正在进行的任务
			for each(userTaskVO in playerData.taskingList)
			{
				userTaskList.push(userTaskVO.taskID);
			}
			
			
			for each(var taskVO :TaskVO in this.taskList)
			{
				if(playerData.playerLevel < taskVO.unlockLevel) continue;
				
				if(userTaskList.indexOf(taskVO.taskID) != -1) continue;

				userTaskVO = null;
				for each(userTaskVO in playerData.completeList)
				{
					if(userTaskVO.netxTaskID == taskVO.taskID)
					{
						result.push(taskVO);
					}
				}
			}
			
			//第一次进游戏，或者进游戏后还没接任务
			if(result.length == 0 && playerData.completeList.length == 0 && playerData.taskingList.length == 0)
			{
				if(this.taskList.length > 0)
				{
					result.push(this.taskList[0]);
				}
			}
			
			result.sort(function (taskVO1 :TaskVO,taskVO2 :TaskVO):int
			{
				var i :int;
				if(!taskVO1.unlockLevel && !taskVO2.unlockLevel)
				{
					i = taskVO1.unlockLevel > taskVO2.unlockLevel ? -1 : taskVO1.unlockLevel == taskVO2.unlockLevel ? 0 : 1;
				}
				return i;
			});
			
			return result;
		}
		/**
		 * 第一次进入游戏是否要显示第一个任务
		 * @return 
		 * 
		 */		
		public function isFirstGame2Task():Boolean
		{
			var playerData :PlayerData = RoleManager.instance.playerData;
			if(!playerData || (playerData.taskingList.length == 0 && 
				playerData.completeList.length == 0 && GameConst.isFirstGame))
			{
				return true;
			}
			
			return false;
		}
		/**
		 * 获取txtOK上的文本组合 
		 * @param taskVO
		 * @param canAccept
		 * @return 
		 * 
		 */		
		public function getStringByTaskVO(taskVO :TaskVO,type :int = TaskVO.IS_ACCEPT,canAccept :Boolean = true,canComplete :Boolean = false):String
		{
			var str :String = "";
			switch(taskVO.taskType)
			{
				case TaskVO.ZHU:
					str += "[主线]";
					break;
				case TaskVO.ZHI:
					str += "[支线]";
					break;
			}
			str += taskVO.taskName;
			
			switch(type)
			{
				case TaskVO.IS_ACCEPT:
					if(canAccept)
					{
						str += "(可接取)";
					}else
					{
						str += "(不可接取)";
					}
					break;
				case TaskVO.IS_COMPLETE:
					if(canComplete)
					{
						str += "(可完成)";
					}else
					{
						str += "(未完成)";
					}
					break;
			}
			
			return str;
		}
		/**
		 * 当前NPC上可以接的任务
		 */		
		public function curNPCCanDoList(npc :MapNPC):Array
		{
			var result :Array = [];
			if(!npc || !npc.npcData) return result;
			
			var taskList :Array = npc.npcData.taskList;
			//已经完成和进行中的任务id列表
			var userTaskList :Array = [];
			var userTaskVO :UserTaskVO;
			var playerData :PlayerData = RoleManager.instance.playerData;
			if(!playerData)
			{
				return result;
			}
			//完成的任务
			for each(userTaskVO in playerData.completeList)
			{
				userTaskList.push(userTaskVO.taskID);
			}
			//正在进行的任务
			for each(userTaskVO in playerData.taskingList)
			{
				userTaskList.push(userTaskVO.taskID);
			}
			
			if(taskList && taskList.length > 0)
			{
				for each(var taskVO :TaskVO in taskList)
				{
					if(playerData.playerLevel < taskVO.unlockLevel) continue;
					
					if(userTaskList.indexOf(taskVO.taskID) != -1) continue;
					
					userTaskVO = null;
					for each(userTaskVO in playerData.completeList)
					{
						if(userTaskVO.netxTaskID == taskVO.taskID)
						{
							result.push(taskVO);
						}
					}
					
				}
			}
			
			if(result.length == 0)
			{
				//第一个任务
				var pd :PlayerData = RoleManager.instance.playerData;
				if(npc.npcData.isFirstTask && pd && pd.taskingList.length == 0 && pd.completeList.length == 0)
				{
					result.push(npc.npcData.taskList[0]);
				}
			}
			return result;
		}
		/**
		 * 查找总表任务 
		 * @param taskID
		 * @return 
		 * 
		 */		
		public function findTaskByID(taskID :int):TaskVO
		{
			for each(var taskVO :TaskVO in this.taskList)
			{
				if(taskVO.taskID == taskID)
				{
					return taskVO;
				}
			}
			
			return null;
		}

	}
}

class PrivateClass{}